// pages/login/login.js
import { Home } from "../../models/home";
import { User } from "../../models/user";

import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
import Base64 from "../../utils/base64";
import { config } from "../../config/config";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    errMsg1: "",
    errMsg2: "",
    errMsg3: "",
    Username: "",
    Nickname: "",
    Password: "",
    config
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.radio = options.radio;
  },

  onChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    this.data.Username = event.detail;
  },
  onBlur(event) {
    this.data.Username = event.detail.value;
  },

  onChange3(event) {
    this.data.Nickname = event.detail;
  },
  onBlur3(event) {
    this.data.Nickname = event.detail.value;
  },

  onChange2(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    this.data.Password = event.detail;
  },
  onBlur2(event) {
    this.data.Password = event.detail.value;
  },

  async confirm() {
    if (this.valid()) {
      let postData = {
        Id: "",
        CreatorId: "",
        CreateTime: new Date(),
        Username: this.data.Username,
        Nickname: this.data.Nickname,
        Password: this.data.Password,
        Email: "",
        Mobile: "",
        Avatar: "",
        Level: 0,
        Gender: 0,
        Birthday: new Date(),
        Bio: "string",
        Money: 0,
        Score: 0,
        Logintime: new Date(),
        Status: "",
        PopedomId: "1290559541989085184",
        RegistType: this.data.radio
      };
      const data = await User.SavaData(postData);
      if (data.Success) {
        Toast("注册成功");
        wx.navigateTo({
          url: "/pages/login/login"
        });
      } else {
        Toast("注册失败");
      }
    }
  },

  valid1() {
    let val = true;
    if (!this.data.Username) {
      val = false;
      this.setData({
        errMsg1: "用户名不能为空"
      });
    } else {
      this.setData({
        errMsg1: ""
      });
    }

    return val;
  },
  valid3() {
    let val = true;
    if (!this.data.Password) {
      val = false;
      this.setData({
        errMsg2: "密码不能为空"
      });
    } else {
      this.setData({
        errMsg2: ""
      });
    }
    return val;
  },

  valid2() {
    let val = true;
    if (!this.data.Nickname) {
      val = false;
      this.setData({
        errMsg3: "昵称不能为空"
      });
    } else {
      this.setData({
        errMsg3: ""
      });
    }
    return val;
  },
  valid() {
    let val1 = this.valid1();
    let val2 = this.valid2();
    let val3 = this.valid3();
    return val1 && val2 && val3;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
